package com.vf.admin.fiwork.util;

import com.vf.admin.common.Constants;
import com.vf.admin.utils.agora.media.RtcTokenBuilder;
import com.vf.admin.utils.agora.media.RtcTokenBuilder.Role;
import com.vf.admin.utils.agora.rtm.RtmTokenBuilder;

/**
 * 创建声网直播和聊天TOKEN
 */
public class AgoraTokenUtil {

    static String appId = Constants.Agora_appId;
    static String appCertificate = Constants.Agora_appCertificate;
    public static String channelName = "";
    public static String userAccount = "";
    public static int expirationTimeInSeconds = 3600;
    static RtcTokenBuilder token = new RtcTokenBuilder();
    //请求声网的URL地址
    static String httpUrl = Constants.Agora_httpUrl;

    /**
     * 根据用户名创建直播TOKEN
     * @return
     */
    public static String buildTokenWithUserAccount(){
        return token.buildTokenWithUserAccount(appId, appCertificate,
                channelName, userAccount, Role.Role_Publisher, createTimestamp());
    }

    /**
     * 根据唯一用户ID创建声网直播TOKEN
     * @param uid
     * @return
     */
    public static String buildTokenWithUid(int uid){
        return token.buildTokenWithUid(appId, appCertificate,
                channelName+uid, uid, Role.Role_Publisher, createTimestamp());
    }

    /**
     * 根据唯一用户ID创建声网直播TOKEN
     * @return
     */
    public static String buildTokenWithUserToken(String channelName,Integer uid){
        return token.buildTokenWithUid(appId, appCertificate,
                channelName, uid, Role.Role_Publisher, createTimestamp());
    }

    /**
     * 根据用户ID创建云信聊天TOKEN
     * @param uid
     * @return
     * @throws Exception
     */
    public static String  rtmTokenBuilder(String uid) throws Exception {
        RtmTokenBuilder token = new RtmTokenBuilder();
        String result = token.buildToken(appId, appCertificate, String.valueOf(uid), RtmTokenBuilder.Role.Rtm_User, 0);
        return result;
    }

    private static int createTimestamp() {
        return (int) (System.currentTimeMillis() / 1000 + expirationTimeInSeconds);
    }
}
